<style>
    .layui-table-cell {height:45px;line-height:unset;}
    .user-box {display:flex;}
    .user-box .avatar{width:45px;height:45px;}
    .user-box .content {margin-left:10px;}
    .user-box .content .account {font-size:18px;color:#000;}
    .user-box .content .sex {font-size:14px;color:red;}
    .user-box .content .info {font-size:14px;margin-top:5px;}
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-form-pane  layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">关键字</label>
                    <div class="layui-input-block">
                        <input type="text" name="keywords" placeholder="请输入账号/昵称/姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select name="status">
                            <option value="">全部状态</option>
                            {volist name="status_list" id="v"}
                            <option value="{$key}">{$v}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <select name="gender">
                            {volist name="gender_list" id="v"}
                            <option value="{$key}">{$v}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="{$lay_btn}">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <button class="layui-btn layuiadmin-btn layui-btn-danger" data-type="del">删除</button>
            <button class="layui-btn layuiadmin-btn" data-type="add">添加新用户</button>
        </div>

        <div class="layui-card-body">
            <table id="{$lay_table}" lay-filter="{$lay_table}"></table>
            <script type="text/html" id="userTpl">
                <div class="user-box">
                    <img src="{{d.avatar ? d.avatar : '__COMMON_IMAGES__/default_avatar.png'}}" class="avatar">
                    <div class="content">
                        <div class="account">{{d.account}}<span class="sex">（{{d.gender_text}}）</span></div>
                        <div class="info">昵称：{{d.nickname}}，真实姓名：{{d.realname}}</div>
                    </div>
                </div>
            </script>
            <script type="text/html" id="setTpl">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="follow"><i class="layui-icon layui-icon-engine"></i>跟进</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['index', 'admin', 'form', 'table'], function () {
        const $ = layui.$;
        const admin = layui.admin;
        const form = layui.form;
        const table = layui.table;
        form.render();

        table.render({
            elem: '#{$lay_table}',
            url: "{:url('user/listUser')}",
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'account', title: '用户信息' ,width: 320, templet: '#userTpl'},
                    {field: 'mobile', title: '手机号', width: 160},
                    {field: 'birthday', title: '生日', width: 160},
                    {field: 'status_text', title: '状态', width: 80, align: 'center'},
                    {field: 'last_login_ip', title: '最后登录IP', width: 160},
                    {field: 'last_login_time', title: '最后登录时间', width: 160},
                    {field: 'create_time', title: '注册时间'},
                    {title: '操作', width: 240, align: 'center', fixed: 'right', toolbar: '#setTpl'}
                ]
            ],
            page: true,
            limit: 50,
            cellMinWidth: 150,
            text: '对不起，加载出现异常！'
        });

        form.on('submit({$lay_btn})', function (data) {
            table.reload('{$lay_table}', {
                where: data.field,
                page: {
                    curr: 1
                }
            });
        });

        //事件
        const active = {
            add: function () {
                const index = layer.open({
                    type: 2,
                    title: '添加用户',
                    content: "{:url('user/userForm')}",
                    maxmin: true,
                    area: ['550px', '550px']
                });
                layer.full(index);
            },
            del: function () {
                const check_data = table.checkStatus('{$lay_table}').data;
                if (check_data.length === 0) {
                    return layer.msg('请选择数据');
                }
                let id_arr = [];
                for (let i = 0; i < check_data.length; i++) {
                    id_arr.push(check_data[i].id);
                }
                layer.confirm('确定删除所选用户吗？', function (index) {
                    admin.req({
                        url: "{:url('user/delUser')}",
                        data: {
                            id_arr: id_arr
                        },
                        done: function (res) {
                            table.reload('{$lay_table}');
                            layer.msg('已删除');
                        }
                    });
                    layer.close(index);
                });
            },
        };

        //监听工具条
        table.on('tool({$lay_table})', function (obj) {
            const data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除此用户吗？', function (index) {
                    admin.req({
                        url: "{:url('user/delUser')}",
                        data: {
                            id: [data.id]
                        },
                        done: function (res) {
                            obj.del();
                            layer.msg('已删除');
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                const index = layer.open({
                    type: 2,
                    title: '编辑用户',
                    content: "{:url('user/userForm')}?id=" + data.id,
                    maxmin: true,
                    area: ['550px', '550px']
                });
                layer.full(index);
            } else if (obj.event === 'follow') {
                const index = layer.open({
                    type: 2,
                    title: '跟进记录',
                    content: "{:url('user/follow')}?id=" + data.id,
                    maxmin: true,
                    area: ['550px', '550px']
                });
                layer.full(index);
            }
        });

        $('.layui-btn.layuiadmin-btn').on('click', function () {
            const type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
